#include "can.h"
#include "ui_can.h"
#include "control.h"
can::can(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::can)
{
    ui->setupUi(this);
        setWindowFlag(Qt::FramelessWindowHint);
}

can::~can()
{
    delete ui;
}

void can::can_play()
{
    char *data;
    extern QSerialPort *serial;
    if(serial->isOpen()==true)
    {
        extern QByteArray getcmd;
        data = strstr(getcmd,"DATA");
        if(data != NULL)
        {
          ui->get_data->setText(data);
        }
    }
    extern int tcp_state;
    if(tcp_state)
    {
        extern QByteArray TCP_buff;
        data = strstr(TCP_buff,"DATA");
        if(data != NULL)
        {
          ui->get_data->setText(data);
        }
    }


}

void can::on_pushButton_3_clicked()
{

    //@EXITID0_STDID1_IDE0_TYPE0_LEN8_SEND_DATA66666666 .
    extern QSerialPort *serial;

    if(serial->isOpen()==true)
    {
        serial->write("@");
        serial->write("EXITID");
        serial->write(ui->EXTID_3->text().toUtf8());
        serial->write("_");

        serial->write("STDID");
        serial->write(ui->STDID_3->text().toUtf8());
        serial->write("_");


        serial->write("IDE");
        if(ui->IDE_STD_3->isChecked()==true)
        {

             serial->write("0");
        }
        else if(ui->IDE_EXTI_3->isChecked()==true)
        {

             serial->write("1");
        }

        serial->write("_");


        serial->write("TYPE");

        if(strcmp(ui->TYPE->currentText().toUtf8(),"DATA")==0)
        {
           serial->write("0");

        }
        else if(strcmp(ui->TYPE->currentText().toUtf8(),"REMOTE")==0)
        {
           serial->write("1");

        }
        serial->write("_");



        serial->write("LEN");
        serial->write(ui->LEN->text().toUtf8());
        serial->write("_");



        serial->write("SEND");
        serial->write("_");



        serial->write("DATA");
        serial->write(ui->SEND_DATA->text().toUtf8());
        serial->write(" ");
        serial->write(".");
    }



    extern int tcp_state;
    extern QTcpSocket *qtcpsocket;
    if(tcp_state == 1)
    {
        qtcpsocket->write("@");
        qtcpsocket->write("EXITID");
        qtcpsocket->write(ui->EXTID->text().toUtf8());
        qtcpsocket->write("_");
        qtcpsocket->write("STDID");
        qtcpsocket->write(ui->STDID->text().toUtf8());
        qtcpsocket->write("_");
        qtcpsocket->write("IDE");
        if(ui->IDE_STD->isChecked()==true)
        {
             qtcpsocket->write("0");
        }
        else if(ui->IDE_EXTI->isChecked()==true)
        {
             qtcpsocket->write("1");
        }
        qtcpsocket->write("_");
        qtcpsocket->write("TYPE");
        if(strcmp(ui->TYPE->currentText().toUtf8(),"DATA")==0)
        {
           qtcpsocket->write("0");
        }
        else if(strcmp(ui->TYPE->currentText().toUtf8(),"REMOTE")==0)
        {
           qtcpsocket->write("1");
        }
        qtcpsocket->write("_");
        qtcpsocket->write("LEN");
        qtcpsocket->write(ui->LEN->text().toUtf8());
        qtcpsocket->write("_");
        qtcpsocket->write("SEND");
        qtcpsocket->write("_");
        qtcpsocket->write("DATA");
        qtcpsocket->write(ui->SEND_DATA->text().toUtf8());
        qtcpsocket->write(" ");
        qtcpsocket->write(".");
    }
}

void can::on_pushButton_4_clicked()
{
    extern QSerialPort *serial;
    extern QTcpSocket *qtcpsocket;
    extern int tcp_state;
    int i;
       for(i=0;i<5;i++)
       {

           if(serial->isOpen()==true)
           {
               serial->write("@");
               serial->write("close");
               serial->write(" ");
               serial->write(".");
           }

       }


       if(tcp_state == 1)
       {
           qtcpsocket->write("@");
           qtcpsocket->write("close");
           qtcpsocket->write(" ");
           qtcpsocket->write(".");
       }
       this->close();
}

void can::on_pushButton_clicked()
{

    //F407
    //@MODE_NORMAL_SJW2_BS18_BS25_PSC6_EXITID0_STDID0_MASKH0_MASKL0_CONFIG .

    //F103
    //@MODE_NORMAL_SJW1_BS19_BS28_PSC4_EXITID0_STDID18_MASKH0_MASKL0_CONFIG .



    extern QSerialPort *serial;
    if(serial->isOpen()==true)
    {
        extern QByteArray getcmd;
        serial->write("@");
        serial->write("MODE");
        serial->write(ui->MODE->currentText().toUtf8());
        serial->write("_");


        serial->write("SJW");
        serial->write(ui->SJW->currentText().toUtf8());
        serial->write("_");

        serial->write("BS1");
        serial->write(ui->BS1->currentText().toUtf8());
        serial->write("_");

        serial->write("BS2");
        serial->write(ui->BS2->currentText().toUtf8());
        serial->write("_");


        serial->write("PSC");
        serial->write(ui->PSC->text().toUtf8());
        serial->write("_");


        serial->write("EXITID");
        serial->write(ui->EXTID->text().toUtf8());
        serial->write("_");

        serial->write("STDID");
        serial->write(ui->STDID->text().toUtf8());
        serial->write("_");


        if(ui->IDE_STD->isChecked()==true)
        {
            serial->write("ADDRESS_STD");

            serial->write(ui->STDID->text().toUtf8());
            serial->write("_");

        }
        else if(ui->IDE_EXTI->isChecked()==true)
        {
             serial->write("ADDRESS_EXD");
             serial->write(ui->EXTID->text().toUtf8());
             serial->write("_");
        }


        serial->write("MASKH");
        serial->write(ui->MASKH->text().toUtf8());
        serial->write("_");


        serial->write("MASKL");
        serial->write(ui->MASKL->text().toUtf8());
        serial->write("_");


        serial->write("CONFIG");
        serial->write(" ");
        serial->write(".");
    }


    extern int tcp_state;
    extern QTcpSocket *qtcpsocket;
    if(tcp_state)
    {
        extern QByteArray TCP_buff;
        qtcpsocket->write("@");
        qtcpsocket->write("MODE");
        qtcpsocket->write(ui->MODE->currentText().toUtf8());
        qtcpsocket->write("_");


        qtcpsocket->write("SJW");
        qtcpsocket->write(ui->SJW->currentText().toUtf8());
        qtcpsocket->write("_");

        qtcpsocket->write("BS1");
        qtcpsocket->write(ui->BS1->currentText().toUtf8());
        qtcpsocket->write("_");

        qtcpsocket->write("BS2");
        qtcpsocket->write(ui->BS2->currentText().toUtf8());
        qtcpsocket->write("_");


        qtcpsocket->write("PSC");
        qtcpsocket->write(ui->PSC->text().toUtf8());
        qtcpsocket->write("_");


        qtcpsocket->write("EXITID");
        qtcpsocket->write(ui->EXTID->text().toUtf8());
        qtcpsocket->write("_");

        qtcpsocket->write("STDID");
        qtcpsocket->write(ui->STDID->text().toUtf8());
        qtcpsocket->write("_");


        if(ui->IDE_STD->isChecked()==true)
        {
            qtcpsocket->write("ADDRESS_STD");
            qtcpsocket->write(ui->STDID->text().toUtf8());
            qtcpsocket->write("_");

        }
        else if(ui->IDE_EXTI->isChecked()==true)
        {
             qtcpsocket->write("ADDRESS_EXD");
             qtcpsocket->write(ui->EXTID->text().toUtf8());
             qtcpsocket->write("_");
        }


        qtcpsocket->write("MASKH");
        qtcpsocket->write(ui->MASKH->text().toUtf8());
        qtcpsocket->write("_");


        qtcpsocket->write("MASKL");
        qtcpsocket->write(ui->MASKL->text().toUtf8());
        qtcpsocket->write("_");


        qtcpsocket->write("CONFIG");
        qtcpsocket->write(" ");
        qtcpsocket->write(".");
    }


}




















